type UtilsTypes = {
  readBuffer(file: File, start: number, end: number): Promise<any>
  //string和String有区别，String被认为是一个类
  getFileSuffix(unit8Array: Uint8Array): string
}

const Utils: UtilsTypes = {
  readBuffer(file, start = 0, end = 2) {
    return new Promise((resolve, reject) => {
      const reader = new FileReader()
      reader.onload = () => {
        resolve(reader.result)
      }

      reader.onerror = () => {
        reject()
      }

      reader.readAsArrayBuffer(file.slice(start, end))
    })
  },
  getFileSuffix(unit8Array) {
    let suffix = ""
    switch (unit8Array.join(" ")) {
      case "137 80 78 71 13 10 26 10":
        suffix = ".png"
        break
      case "47 49 46 38 39(37) 61":
        suffix = "gif"
        break
      case "ff d8 ff":
        suffix = "jpeg"
        break
      case "ff d9 ff":
        suffix = ".jpg"
        break
      case "ff d9 ff":
        suffix = ".jpg"
        break
      default:
        break
    }
    return suffix
  },
}
export default Utils
